package com.codename1.samples;

import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Graphics;
import com.codename1.ui.Stroke;
import com.codename1.ui.Transform;
import com.codename1.ui.geom.GeneralPath;
import com.codename1.ui.geom.Rectangle;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class ShapeClipTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        

        Form hi = new Form("Shape Clip");

// We create a 50 x 100 shape, this is arbitrary since we can scale it easily
        GeneralPath path = new GeneralPath();
        path.moveTo(20, 0);
        path.lineTo(30, 0);
        path.lineTo(30, 100);
        path.lineTo(20, 100);
        path.lineTo(20, 15);
        path.lineTo(5, 40);
        path.lineTo(5, 25);
        path.lineTo(20, 0);

        Stroke stroke = new Stroke(0.5f, Stroke.CAP_ROUND, Stroke.JOIN_ROUND, 4);
        hi.getContentPane().getUnselectedStyle().setBgPainter((Graphics g, Rectangle rect) -> {
            g.setColor(0xff);
            float widthRatio = ((float) rect.getWidth()) / 50f;
            float heightRatio = ((float) rect.getHeight()) / 100f;
            g.scale(widthRatio, heightRatio);
            g.translate((int) (((float) rect.getX()) / widthRatio), (int) (((float) rect.getY()) / heightRatio));
            Transform t = g.getTransform();
            t.rotate((float)Math.PI/4, rect.getWidth()/2/widthRatio, rect.getHeight()/2/heightRatio);
            g.setTransform(t);
            g.setClip(path);
            g.setAntiAliased(true);
            g.setColor(0x00ff00);
            g.fillRect(0, 0, 50, 100);
            g.setClip(path.getBounds());
            g.drawShape(path, stroke);
            g.translate(-(int) (((float) rect.getX()) / widthRatio), -(int) (((float) rect.getY()) / heightRatio));
            g.resetAffine();
        });

        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}
